// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import ViewMgr from '../manager/ViewMgr';
import { PageViewTypeId } from '../define/PageDefine';
import Decoractor from '../core/Decoractor';

const { ccclass, property } = cc._decorator;

@ccclass
export default class BaseUIView extends cc.Component implements UIView {
  zType = 0;
  protected viewName: PageViewTypeId = PageViewTypeId.BaseUIView;
  onLoad() {
    this.registerEvents();
    this.node.on(<any>cc.Node.EventType.TOUCH_END, () => {}, this);
  }

  @property(cc.Node)
  coverNode: cc.Node = null;

  start() {}
  public viewData: any;
  onShow(data: any = null) {
    this.viewData = data;
  }

  onHide() {}

  public onClickCloseBtn(): void {
    ViewMgr.ins.closeView(this.viewName);
  }

  protected onPlayOpenEftOver(): void {}

  public onOpenJelly(): void {
    this.coverNode.scale = 0;
    let action = cc.scaleTo(0.3, 1).easing(cc.easeBackOut());
    this.coverNode.runAction(action);
  }

  protected registerEvents(): void {
    let eventMap = this.getEventMap();

    if (eventMap) {
      Object.keys(eventMap).forEach((key) => {
        for (let i: number = 0; i < eventMap[key].length; i++) {
          let methodName: string = eventMap[key][i];
          let callback = this[methodName];
          if (callback) {
            ViewMgr.ins.addEventListener(key, callback, this);
            //core.EventMgr.inst.addEventListener(key, callback, this);
          }
        }
      });
    }
  }

  protected getEventMap(): Object {
    let eventMap = null;
    let className = this['__proto__'].__classname__;
    if (className) {
      let obj = Decoractor.classEventMap[className];
      if (obj) {
        if (!eventMap) eventMap = {};
        (<any>Object).assign(eventMap, obj);
      }
    }
    return eventMap;
  }

  // update (dt) {}
}
